package com.wrh.draw.layer;

import com.wrh.draw.LonLatConverter;
import com.wrh.draw.util.Graphics2DUtil;
import org.geojson.FeatureCollection;
import org.geojson.GeoJsonObject;
import org.geojson.MultiPolygon;

import java.awt.*;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wrh
 */
public class BoundaryLayer extends GeoJsonLayer {

    public BoundaryLayer(FeatureCollection featureCollection) {
        super(featureCollection);
    }

    public static BoundaryLayer form(FeatureCollection featureCollection) {
        return new BoundaryLayer(featureCollection);
    }

    @Override
    public void draw(Graphics2D g, LonLatConverter lonLatConverter) {
        g.setColor(Color.BLACK);
        GeoJsonObject geoJsonObject = this.getFeatureCollection().getFeatures().get(0).getGeometry();
        if (geoJsonObject instanceof MultiPolygon) {
            ((MultiPolygon) geoJsonObject).getCoordinates().forEach(coordinate -> {
                List<Point> pointList = coordinate.get(0).stream().map(lonLat ->
                        lonLatConverter.lonLatToPoint(lonLat.getLongitude(), lonLat.getLatitude())).collect(Collectors.toList());
                Graphics2DUtil.drawPolygon(g, pointList);
            });
        }
    }

}
